<?php
class forumclass {

	function forumclass() {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}
	
	function forum_add($name){
		if (strlen($name) > 0){
			$name = trim($name);
			$name = strip_tags($name);
			$name = addslashes($name);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_groups` WHERE `name`='".$name."'", __LINE__, __FILE__);		
			if (mysql_num_rows($sql) < 1){
				$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."forum_groups` WHERE `parent`=0", __LINE__, __FILE__);						
				if (mysql_num_rows($sql) < 1) $pos = 0;
				else {
					$row = mysql_fetch_array($sql);
					$pos = $row[0] + 1;
				}
				$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."forum_groups` (`id`,`parent`,`name`,`type`,`pos`) VALUES ('', 0, '".$name."', '', ".$pos.")", __LINE__, __FILE__);
				return 1;
			}
		}
		return 0;
	}
	
	function forum_del($arr){
		$ids = array();
		foreach($arr as $elem){
			if (isset($elem) and ($elem > 0)){
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."forum_groups` WHERE ".$elem." = `id`", __LINE__, __FILE__);		
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."forum_groups` WHERE ".$elem." = `parent`", __LINE__, __FILE__);		
				$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$elem." = `parent` OR ".$elem."=`id`", __LINE__, __FILE__);		
				while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
					$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$row['id']." = `parent`", __LINE__, __FILE__);		
				}
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$elem." = `parent`", __LINE__, __FILE__);		
			}
		}
	}
	
	function forum_mod($parent, $name){
		if (isset($parent) && (strlen($parent) > 0)){
			if (!isset($name) && (strlen($name) == 0)) return 5;
			$name = trim($name);
			$name = strip_tags($name);
			$name = addslashes($name);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_groups` WHERE `name`='".$name."'", __LINE__, __FILE__);		
			if (mysql_num_rows($sql) < 1){
				$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."forum_groups` SET `name`='".$name."' WHERE ".$parent." = `id`", __LINE__, __FILE__);
				return 1;
			}
		}
		return 0;
	}
	
	function get_all_forums(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_groups` ORDER BY `pos`", __LINE__, __FILE__);	
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$row['name'] = stripslashes($row['name']);
			$result[$row['id']] = $row;
		}
		return $result;
	}
	
	function get_forum_data($group_id){
		$result = array();
		$group_id = intval($group_id);
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_groups` WHERE ".$group_id." = `id`", __LINE__, __FILE__);			
		if (mysql_num_rows($sql) > 0) {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$row['name'] = stripslashes($row['name']);
			$result = $row;
		}
		
		$result['totals']['themes'] = 0;
		$result['totals']['post'] = 0;
		
		$sql = $this->mysqlclass->mysql_my_query("SELECT *, UNIX_TIMESTAMP(`time_modif`) `time_modif`, UNIX_TIMESTAMP(`time_create`) `time_create` FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$group_id." = `parent` ORDER BY `pos`", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			$result['totals']['themes'] ++;
			$result['totals']['post'] += $row['post_count'];
			$row['name'] = stripslashes($row['name']);
			if (!isset($result['themes']['newest'])) $result['themes']['newest']['data'] = $row;
			else {
				if ($result['themes']['newest']['data']['time_modif'] > date("Y-m-d H:m:s", $row['time_modif'])){
					$result['themes']['newest']['data'] = $row;
				}
			}
			if (!isset($result['themes']['oldest'])) $result['themes']['oldest']['data'] = $row;
			else {
				if ($result['themes']['oldest']['data']['time_modif'] < date("Y-m-d H:m:s", $row['time_modif'])){
					$result['themes']['oldest']['data'] = $row;
				}
			}
		}

		if (isset($result['themes']['newest']['data']['id'])){
			$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$result['themes']['newest']['data']['id']." = `parent` AND `name`='post_date' ORDER BY `value` ASC", __LINE__, __FILE__);
			if (mysql_num_rows($sql2) > 0) {
				$row2 = mysql_fetch_array($sql2, MYSQL_ASSOC);
				$sql3 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$result['themes']['newest']['data']['id']." = `parent` AND `post`=".$row2['post']." ORDER BY `pos`", __LINE__, __FILE__);
				while ($row3 = mysql_fetch_array($sql3, MYSQL_ASSOC)) {
					$row3['name'] = stripslashes($row3['name']);
					$row3['value'] = stripslashes($row3['value']);
					$result['themes']['newest']['newest_post'][$row3['name']] = $row3['value'];
				}
			}
			$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$result['themes']['newest']['data']['id']." = `parent` AND `name`='post_date' ORDER BY `value` DESC", __LINE__, __FILE__);
			if (mysql_num_rows($sql2) > 0) {
				$row2 = mysql_fetch_array($sql2, MYSQL_ASSOC);
				$sql3 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$result['themes']['newest']['data']['id']." = `parent` AND `post`=".$row2['post']." ORDER BY `pos`", __LINE__, __FILE__);
				while ($row3 = mysql_fetch_array($sql3, MYSQL_ASSOC)) {
					$row3['name'] = stripslashes($row3['name']);
					$row3['value'] = stripslashes($row3['value']);
					$result['themes']['newest']['oldest_post'][$row3['name']] = $row3['value'];
				}
			}
		}

		if (isset($result['themes']['oldest']['data']['id'])){
			$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$result['themes']['oldest']['data']['id']." = `parent` AND `name`='post_date' ORDER BY `value` ASC", __LINE__, __FILE__);
			if (mysql_num_rows($sql2) > 0) {
				$row2 = mysql_fetch_array($sql2, MYSQL_ASSOC);
				$sql3 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$result['themes']['oldest']['data']['id']." = `parent` AND `post`=".$row2['post']." ORDER BY `pos`", __LINE__, __FILE__);
				while ($row3 = mysql_fetch_array($sql3, MYSQL_ASSOC)) {
					$row3['name'] = stripslashes($row3['name']);
					$row3['value'] = stripslashes($row3['value']);
					$result['themes']['oldest']['newest_post'][$row3['name']] = $row3['value'];
				}
			}
			$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$result['themes']['oldest']['data']['id']." = `parent` AND `name`='post_date' ORDER BY `value` DESC", __LINE__, __FILE__);
			if (mysql_num_rows($sql2) > 0) {
				$row2 = mysql_fetch_array($sql2, MYSQL_ASSOC);
				$sql3 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$result['themes']['oldest']['data']['id']." = `parent` AND `post`=".$row2['post']." ORDER BY `pos`", __LINE__, __FILE__);
				while ($row3 = mysql_fetch_array($sql3, MYSQL_ASSOC)) {
					$row3['name'] = stripslashes($row3['name']);
					$row3['value'] = stripslashes($row3['value']);
					$result['themes']['oldest']['oldest_post'][$row3['name']] = $row3['value'];
				}
			}
		}


		return $result;
	}
	
	function get_post_fields(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_discribe` WHERE 'post'=`type`", __LINE__, __FILE__);	
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $result[$row['id']] = $row;
		return $result;
	}

	function get_forum_subjects($group_id){
		$group_id = intval($group_id);
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$group_id." = `parent` ORDER BY `pos`", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			$row['name'] = stripslashes($row['name']);
			$result[$row['id']] = $row;
			$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$row['id']." = `parent` AND `name`='post_date' ORDER BY `value` ASC", __LINE__, __FILE__);
			if (mysql_num_rows($sql2) > 0) {
				$row2 = mysql_fetch_array($sql2, MYSQL_ASSOC);
				$sql3 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$row['id']." = `parent` AND `post`=".$row2['post']." ORDER BY `pos`", __LINE__, __FILE__);
				while ($row3 = mysql_fetch_array($sql3, MYSQL_ASSOC)) {
					$row3['name'] = stripslashes($row3['name']);
					$row3['value'] = stripslashes($row3['value']);
					$result[$row['id']]['data']['newest'][$row3['name']] = $row3['value'];
				}
			}
			$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$row['id']." = `parent` AND `name`='post_date' ORDER BY `value` DESC", __LINE__, __FILE__);
			if (mysql_num_rows($sql2) > 0) {
				$row2 = mysql_fetch_array($sql2, MYSQL_ASSOC);
				$sql3 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$row['id']." = `parent` AND `post`=".$row2['post']." ORDER BY `pos`", __LINE__, __FILE__);
				while ($row3 = mysql_fetch_array($sql3, MYSQL_ASSOC)) {
					$row3['name'] = stripslashes($row3['name']);
					$row3['value'] = stripslashes($row3['value']);
					$result[$row['id']]['data']['oldest'][$row3['name']] = $row3['value'];
				}
			}
		}
		return $result;
	}
	
	function subject_pos($group_id, $subject_id, $pos){
		$group_id = intval($group_id);
		if ($pos == 'up') {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$subject_id." = `id`", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0){
				$subject1 = mysql_fetch_array($sql, MYSQL_ASSOC);
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$group_id." = `parent` AND `pos` < ".$subject1['pos']." ORDER BY `pos` DESC", __LINE__, __FILE__);
				if (mysql_num_rows($sql) > 0){
					$subject2 = mysql_fetch_array($sql, MYSQL_ASSOC);
					if ((isset($subject1['pos'])) and (isset($subject2['pos'])) and (strlen($subject1['pos'])>0) and (strlen($subject2['pos'])>0)){
						$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."forum_catalog` SET `pos`=".$subject2['pos']." WHERE `id` = ".$subject1['id'], __LINE__, __FILE__);
						$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."forum_catalog` SET `pos`=".$subject1['pos']." WHERE `id` = ".$subject2['id'], __LINE__, __FILE__);
					}
				}
			}
		} elseif ($pos == 'down'){
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$subject_id." = `id`", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0){
				$subject1 = mysql_fetch_array($sql, MYSQL_ASSOC);
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$group_id." = `parent` AND `pos` > ".$subject1['pos']." ORDER BY `pos` ASC", __LINE__, __FILE__);
				if (mysql_num_rows($sql) > 0){
					$subject2 = mysql_fetch_array($sql, MYSQL_ASSOC);
					if ((isset($subject1['pos'])) and (isset($subject2['pos'])) and (strlen($subject1['pos'])>0) and (strlen($subject2['pos'])>0)){
						$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."forum_catalog` SET `pos`=".$subject2['pos']." WHERE `id` = ".$subject1['id'], __LINE__, __FILE__);
						$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."forum_catalog` SET `pos`=".$subject1['pos']." WHERE `id` = ".$subject2['id'], __LINE__, __FILE__);
					}
				}
			}		
		}
	}
	
	function forum_subject_add($parent, $array, $lang){
		$parent = intval($parent);
		foreach($array as $key=>$arr) $array[$key] = trim($array[$key]);
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$parent." = `parent`", __LINE__, __FILE__);			
		if (mysql_num_rows($sql) > 0) {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$pos = $row['pos'] + 1;
		} else $pos = 1;
		$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."forum_catalog` (`id`, `name`, `pos`, `parent`, `time_create`, `time_modif`, `post_count`, `author`) VALUES ('', '".$array['post_subject']."', ".$pos.", ".$parent.", '".date("YmdHis")."', '".date("YmdHis")."', 0, '".$array['post_name']."')", __LINE__, __FILE__);
		$new_p = mysql_insert_id();
		if ($new_p > 0){
			$post_fields = $this->get_post_fields();
			foreach($post_fields as $field){
				if ((isset($array[$field['name']])) && (strlen($array[$field['name']])>0)){
					$this->mod_post_fields($new_p, 1, $field, $array[$field['name']], $lang);
					if (!(isset($is_insert))) {
						$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."forum_catalog` SET `post_count`=1 WHERE `id` =".$new_p, __LINE__, __FILE__);
						$is_insert = 1;
					}
				}
			}
			// file - image insert
		}
	}
	
	function mod_post_fields($parent, $post, $array, $value, $lang){
		if (!isset($parent) or (strlen($parent) < 1)) return NULL;
		$parent = intval($parent);
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$parent." = `parent` AND `name`='".$array['name']."' AND `lang`='".$lang."' AND `post`=".$post, __LINE__, __FILE__);					
		if (mysql_num_rows($sql) > 0) {
			$value = strip_tags($value);
			$value = addslashes($value);
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."forum_data` SET `value`='".$value."' WHERE ".$row['id']." = `id`", __LINE__, __FILE__);
		} else {
			$value = addslashes($value);
			$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."forum_data` (`id`, `post`, `parent`, `name`, `value`, `type`, `pos`, `lang`) VALUES ('', ".$post.", ".$parent.", '".$array['name']."', '".$value."', '".$array['type_of_value']."', '".$array['pos']."', '".$lang."')", __LINE__, __FILE__);
		}
	}
	
	function array_sort_desc($a, $b){
		if ($a['post_date']['value'] == $b['post_date']['value']) return 0;
		return ($a['post_date']['value'] > $b['post_date']['value']) ? -1 : 1;
	}
		
	function get_subject_data($subject_id, $sort="+"){
		$result = array();
		$subject_id = intval($subject_id);
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$subject_id." = `id` ORDER BY `time_modif` DESC", __LINE__, __FILE__);			
		if (mysql_num_rows($sql) > 0) {
			$result = mysql_fetch_array($sql, MYSQL_ASSOC);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$subject_id." = `parent`", __LINE__, __FILE__);			
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
				$row['value'] = stripslashes($row['value']);
				$result['data'][$row['post']][$row['name']] = $row;
			}
		}
		if ($sort == '-') usort($result['data'], array($this, "array_sort_desc"));
		return $result;
	}
	
	function forum_post_add($parent, $array, $lang){
		$parent = intval($parent);
		$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."forum_catalog` SET `post_count`=`post_count`+1, `time_modif`=".date("YmdHis")." WHERE ".$parent." = `id`", __LINE__, __FILE__);
		$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`post`) FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$parent." = `parent` AND `lang`='".$lang."'", __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0){
			$rows = mysql_fetch_array($sql);
			$post = $rows[0] + 1;
		} else $post = 1;
		$post_fields = $this->get_post_fields();
		foreach($post_fields as $field){
			if ((isset($array[$field['name']])) && (strlen($array[$field['name']])>0)){
				$this->mod_post_fields($parent, $post, $field, $array[$field['name']], $lang);
			}
		}
		// file - image insert
	}
	
	function post_del($parent, $array){
		$parent = intval($parent);
		foreach($array as $elem){
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$parent." = `parent` AND ".$elem."=`post`", __LINE__, __FILE__);		
		}
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$parent." = `parent`", __LINE__, __FILE__);		
		if (mysql_num_rows($sql) == 0){
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$parent." = `id`", __LINE__, __FILE__);		
		}
	}
	
	function subject_del($array){
		foreach($array as $elem){
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."forum_catalog` WHERE ".$elem." = `id`", __LINE__, __FILE__);		
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."forum_data` WHERE ".$elem." = `parent`", __LINE__, __FILE__);		
		}
	}
}
?>